﻿using DataRepositoryLayer.Classes;
using DataRepositoryLayer.Classes;
using DataRepositoryLayer.Interfaces;
using ModelLayer;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace BusinessLogicLayer
{
    public class BatteryManager
    {
        public IList<Battery> GetAllBatteries()
        {
            IList<Battery> batteries = null;
            using (IUnitOfWork context = new UnitOfWork())
            {
                IBatteryRepository batteryRep = new BatteryRepository(context);
                batteries = batteryRep.GetAll();
                return batteries;
            }
        }

        public void AddNewBattery(Battery newBattery)
        {
            using (IUnitOfWork context = new UnitOfWork())
            {
                IBatteryRepository batteryRep = new BatteryRepository(context);
                batteryRep.Add(newBattery);
                context.Commit();
            }
        }

        public void DeleteBattery(Battery battery)
        {
            using (IUnitOfWork context = new UnitOfWork())
            {
                IBatteryRepository batteryRep = new BatteryRepository(context);
                batteryRep.Remove(battery);
                context.Commit();
            }
        }

        public void UpdateBattery(Battery battery)
        {
            using (IUnitOfWork context = new UnitOfWork())
            {
                IBatteryRepository batteryRep = new BatteryRepository(context);
                batteryRep.Update(battery);
                context.Commit();
            }
        }

        public void AddNewBatteryType(BatteryType type)
        {
            using(IUnitOfWork context = new UnitOfWork())
            {
                context.BeginTransaction(IsolationLevel.Serializable);
                IBatteryTypeRepository typeRep = new BatteryTypeRepository(context);
                typeRep.Add(type);
                context.Commit();
                IStationRepository statRep = new StationRepository(context);
                IList<Station> allStations = statRep.GetAll();
                IList<Pricing> pricings = new List<Pricing>();
                foreach(Station s in allStations)
                {
                    pricings.Add(new Pricing() { price = 0, station_id = s.id, battery_type_id = type.id });
                }
                IPricingRepository pricingRep = new PricingRepository(context);
                pricingRep.AddMultiple(pricings.ToArray());
                context.Commit();
                context.CommitTransaction();
            }
        }

        public void UpdateBatteryType(BatteryType type)
        {
            using(IUnitOfWork context = new UnitOfWork())
            {
                IBatteryTypeRepository typeRep = new BatteryTypeRepository(context);
                typeRep.Update(type);
                if(type.Pricings != null)
                {
                    IPricingRepository pricingRep = new PricingRepository(context);
                    pricingRep.UpdateMultiple(type.Pricings.ToArray());
                }
                context.Commit();
            }
        }

        public void DeleteBatteryType(BatteryType type)
        {
            using(IUnitOfWork context = new UnitOfWork())
            {
                IBatteryTypeRepository typeRep = new BatteryTypeRepository(context);
                typeRep.Remove(type);
                context.Commit();
            }
        }
    }
}
